package actions;

import java.sql.SQLException;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.RowSet;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import db.DbException;
import forms.AdmitStatusChangeForm;
import model.AdminModel;

public class AdminChangeAdmitStatusAction extends Action {

	private static final boolean DEBUG = true;

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws DbException {
				
		// get username and status change we want from the form
		AdmitStatusChangeForm aform = (AdmitStatusChangeForm)form;
		String user_id = aform.getUser();
		String updatedStatus = aform.getUpdatedStatus();
		
		if (DEBUG) {
			System.err.println("AdminChangeAdmitStatusAction: user_id(" + user_id +")");
			System.err.println("AdminChangeAdmitStatusAction: updatedStatus(" + updatedStatus +")");
		}

		// Update the application
		int id = Integer.parseInt(user_id);
		AdminModel.updateApplicationStatus(id, updatedStatus);
		
		Vector<String> app_id = (Vector<String>)request.getSession().getAttribute("app_id");
		Vector<String> statuses = (Vector<String>)request.getSession().getAttribute("statuses");
		
		int updateIndex = app_id.indexOf(user_id);
		statuses.set(updateIndex, updatedStatus);
		
		request.getSession().setAttribute("statuses", statuses);
		
		return mapping.findForward("success");
	}
}